package dnbW.dbW.sucxentdbW;

import dnbW.dbW.DataSource;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author truongbaquan
 * Encapsulates an ID path that decorates LeafPath (Decorator Pattern)
 */
public class IdPath implements ILeafPath {
    // <editor-fold defaultstate="collapsed" desc="Decorator Pattern">
    private ILeafPath leafPath;

    @Override
    public int getPathId() {
        return this.leafPath.getPathId();
    }

    @Override
    public DataSource getDataSource() {
        return this.leafPath.getDataSource();
    }

    @Override
    public String[] getElements() {
        return this.leafPath.getElements();
    }

    @Override
    public String getElementAtLevel(int level) {
        return this.leafPath.getElementAtLevel(level);
    }

    @Override
    public boolean isAttribute() {
        return this.leafPath.isAttribute();
    }

    @Override
    public int getLevel() {
        return this.leafPath.getLevel();
    }

    @Override
    public String toSQLPathExp() {
        return this.leafPath.toSQLPathExp();
    }

    @Override
    public String toPathExp() {
        return this.leafPath.toPathExp();
    }

    @Override
    public int compareTo(Object o) {
        return this.leafPath.compareTo(o);
    }
    // </editor-fold>

    private Map<IdRefPath, IdRelation> relations = new HashMap<IdRefPath, IdRelation>();

    public Collection<IdRelation> getRelations() {
        return relations.values();
    }

    public IdRelation getRelation(IdRefPath idRef) {
        return relations.get(idRef);
    }

    public IdRelation addIdRef(IdRefPath idRef) {
        IdRelation relation = new IdRelation(this, idRef);
        this.relations.put(idRef, relation);
        idRef.addRelation(relation);
        return relation;
    }

    public IdPath(ILeafPath leafPath) {
        this.leafPath = leafPath;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final IdPath other = (IdPath) obj;
        if (this.leafPath != other.leafPath && (this.leafPath == null || !this.leafPath.equals(other.leafPath))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 37 * hash + (this.leafPath != null ? this.leafPath.hashCode() : 0);
        return hash;
    }
}
